<template>
    <el-dialog :title="title" :visible.sync="open" width="65%" append-to-body :before-close="closeDelDialog" >
        <div style=" height: 700px;overflow-y: auto;width:80%;margin: auto;">
            <addField v-if="infoList.length > 0" :infoList="infoList" :inputParams="inputParams" @onChangeTap="onChangeTap" >
            </addField>
            <el-empty v-else description="暂无著录数据" class="leftMain"></el-empty>
        </div>
        <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="closeDelDialog">取消</el-button>
            <!--isParams==1 离线接收显示 -->
            <el-button type="primary" v-if="isParams == 1 && openInfo.ids == undefined"
                @click="submitDetail(1)">继承追加</el-button>
            <el-button type="primary" @click="submitDetail">确定</el-button>
        </div>

    </el-dialog>
</template>
<script>

import {
  getaddfield,
  addProject,
  updateItem,
  mergeFolder,
  getupdatefield,
  getItemById,
  updateItem1
} from "@/api/archive/fileCollection/offlinReception.js";
import addField from "@/views/archive/fileCollection/offlinReception/addField.vue"  //著录字段列表
import { addDaProject, updateDaProject } from "@/api/archive/digitalizeReceive/archiveReception";
import {addZjItem} from "@/api/archive/digitalizeReceive/archivesCollection"; // 数字化成果接收--档案接收
import { addDaProjectjsgl } from "@/api/archive/digitalizeReceive/receivieManagement"; // 数字化成果接收--接收管理
export default {
    components: {
        addField,
    },
    props: ["open", "openInfo", "isParams"],
    data() {
        return {
            inputParams: "", //点击修改时获取的查询框的数据
            lists: [],
            infoList: [],
            inputList: [],
            // 标题
            title: '',
        }
    },
    watch: {
        openInfo: {
            handler(valNew) {
                if (valNew.isDisabled == 3) { //组项目
                    this.title = '组项目'
                    this.getaddfieldPost({
                        categoryId: valNew.categoryId,
                        menuId: valNew.menuId,
                        type: valNew.type
                    })
                } else {
                    if (valNew.ids == undefined) { //添加
                        this.title = '项目著录'
                        this.getaddfieldPost({
                            categoryId: valNew.categoryId,
                            menuId: valNew.menuId,
                            type: valNew.type
                        })
                    } else {
                        //修改
                        this.title = '项目修改'
                        this.getUpdatefieldPost(valNew)
                    }
                }

            },
            immediate: true
        },
    },
    created() {

    },
    methods: {
        //获取著录字段
        getaddfieldPost(data) {
            this.$modal.loading("加载中...")
            getaddfield(data).then(response => {
                this.lists = response.data.addColumn;
                var listData = response.data.addColumn;
                let dataInfo = {};
                let arr = {};
                listData.forEach((item, index) => {
           
                    if (item.COL_WIDTH != null||item.COL_WIDTH>=0) {
                                    item.COL_WIDTH = item.COL_WIDTH <= 240 ? 240 : 610;
                                }
                    arr[item.FIELD_NAME] = item.DEFAULT_VALUE // 根据自己需求可以拼接
                    let { ZSFL } = item;
                    if (!dataInfo[ZSFL]) {
                        dataInfo[ZSFL] = {
                            ZSFL,
                            child: []
                        }
                    }
                    dataInfo[ZSFL].child.push(item);
                });
                this.setSessionStorage(this.lists)
                this.infoList = Object.values(dataInfo); // list 转换成功的数据
                this.inputParams = {};  //添加时传{}
                this.inputParams = arr;  //修改获取的输入框参数
                this.inputList = arr; //修改时用来判断提示窗是否有值
                this.$modal.closeLoading()
            });
        },
        //获取修改字段
        getUpdatefieldPost(data) {
            this.$modal.loading("加载中...")
            getupdatefield({
                categoryId: data.categoryId,
                menuId: data.menuId,
                type: data.type
            }).then(response => {
                if (response.data.updateColumn.length > 0) {
                    this.lists = response.data.updateColumn;
                    var listData = response.data.updateColumn;
                    let dataInfo = {};
                    listData.forEach((item, index) => {
                        if (item.COL_WIDTH != null||item.COL_WIDTH>=0) {
                                    item.COL_WIDTH = item.COL_WIDTH <= 240 ? 240 : 610;
                                }
                        let { ZSFL } = item;
                        if (!dataInfo[ZSFL]) {
                            dataInfo[ZSFL] = {
                                ZSFL,
                                child: []
                            }
                        }
                        dataInfo[ZSFL].child.push(item);
                    });
                    this.setSessionStorage(this.lists)
                    this.infoList = Object.values(dataInfo); // list 转换成功的数据
                    this.getYqdaByIdPost(data, this.lists)
                    this.$modal.closeLoading()
                }
            });

        },
        // //获取的查询添加list  改成["MJ","BGQX"] 用于动态显示下拉用方法
        setSessionStorage(valNew) {
            var data = [];
            for (var i in valNew) {
                if (valNew[i].SHOWTYPE == 5) {
                    data.push(valNew[i].FIELD_NAME)
                }
            }
            sessionStorage.setItem("data", JSON.stringify(data))
        },

        /**
         * 修改字段
         * 获取组卷详情**/
        getYqdaByIdPost(data, lists) {
            var column = [];
            for (var i in lists) {
                if (lists[i].SHOWTYPE == 7 || lists[i].SHOWTYPE == 8) {
                    column.push(lists[i].FIELD_NAME + '_ID')
                }
                column.push(lists[i].FIELD_NAME)
            }
            getItemById({
                categoryId: data.categoryId,
                id: data.ids,
                column: column.toString()
            }
            ).then(res => {
                this.inputParams = res.data;  //修改获取的输入框参数
                this.inputList = res.data; //修改时用来判断提示窗是否有值
            })
        },
        //关闭
        closeDelDialog() {
            this.$emit("closeDelDialog")
        },
        //获取input 输入框字段
        onChangeTap(data) {
       
            if (data.val != undefined) {
                this.inputList = data.queryParams;
            } else {
                this.inputList = data;
            }

           
           
        },

        //确定
        submitDetail(val) {
            //将数据从新排序为了弹窗提示做准备
            // var arr = [];
            // for (var i in this.infoList) {
            //     for (var j in this.infoList[i].child) {
            //         if (this.infoList[i].child[j].IS_NO_EMPTY != 0) {
            //             arr.push(this.infoList[i].child[j])
            //         }
            //     }
            // }
            // var column = [];
            // //判断输入框是否有输入值，如果有值删除当前数据
            // for (var i in this.inputList) {
            //     column.push({
            //         name: i,
            //         value: this.inputList[i],
            //     })
            //     for (var d in arr) {
            //         if (arr[d].FIELD_NAME == i) {
            //             arr.splice(d, 1);
            //             //  this.$delete(arr, k);
            //         }
            //     }
            // }
            // //删除后的arr数据进行弹窗提示
            // if (arr.length > 0) {
            //     for (var k in arr) {
            //         this.$message({ message: '请输入' + arr[k].FIELD_CAPTION + '!', type: 'warning' })
            //         return
            //     }
            // }
             //将数据从新排序为了弹窗提示做准备
             var arr = [];
            var arrList = [];
            console.log(this.infoList, "liuhairong 1")
            for (var i in this.infoList) {
                for (var j in this.infoList[i].child) {

                    arrList.push(this.infoList[i].child[j]);
                    if (this.infoList[i].child[j].IS_NO_EMPTY != 0) {
                        arr.push(this.infoList[i].child[j])
                    }
                }
            }

            var column = [];

            for (var n in arr) {
                for (var k in this.inputList) {
                    if (arr[n].FIELD_NAME == k) {
                        arr[n].DEFAULT_VALUE = this.inputList[k]
                    }
                }
                console.log(arr,this.inputList,"sjdfdlksfjsdklj")
                if (arr[n].DEFAULT_VALUE == undefined || arr[n].DEFAULT_VALUE == null) {
                    this.$message({ message: '请输入' + arr[n].FIELD_CAPTION + '!', type: 'warning' })
                    return
                }
                var DEFAULT_VALUE_INPUT = arr[n].DEFAULT_VALUE.toString(); //trim()方法需要转成成String格式
                var inputValue = DEFAULT_VALUE_INPUT.trim(); // 去除首尾空格

                if (inputValue === '') {
                    this.$message({ message: '输入' + arr[n].FIELD_CAPTION + '不能全部为空!', type: 'warning' })
                    return
                }
            }

            for (var t in arrList) {
                for (var v in this.inputList) {
                    if (arrList[t].FIELD_NAME == v) {
                        arrList[t].DEFAULT_VALUE = this.inputList[v]
                    }
                }


            }

            for (var i in this.inputList) {
                column.push({
                    name: i,
                    value: this.inputList[i],
                })
            }

            if (this.openInfo.isDisabled == 3) {
                mergeFolder({
                    categoryId: this.openInfo.categoryId,
                    folderIds: this.openInfo.ids,
                    column: column
                }).then(res => {
                    this.$modal.msgSuccess("操作成功");
                    this.$emit("submitDetail")
                })
            } else {
                //离线接收isParams=1  数字化成果档案接收isParams=2   文件整理
                if (this.isParams == 1) {
                    this.$modal.loading("加载中...")
                    if (this.openInfo.ids != undefined) {
                        //修改
                        updateItem(
                            {
                                categoryId: this.openInfo.categoryId,
                                id: this.openInfo.ids,
                                column: column
                            }
                        ).then(res => {
                            this.$modal.closeLoading()
                            this.$modal.msgSuccess("修改成功");
                            this.infoList = [];
                            this.value = [];
                            this.uploadValue = undefined
                            this.$emit("submitDetail")
                        }).catch(res => {
                            this.$modal.closeLoading()
                        })
                    } else {
                        //添加
                        addProject(
                            {
                                categoryId: this.openInfo.categoryId,
                                column: column
                            }
                        ).then(res => {
                            this.$modal.closeLoading()
                            this.$modal.msgSuccess("著录成功");



                            if (val == 1) {

                                this.$emit("submitTooDetail") //继承追加
                            } else {
                                this.infoList = [];
                                this.value = [];
                                this.uploadValue = undefined
                                this.$emit("submitDetail")
                            }
                        }).catch(res => {
                            this.$modal.closeLoading()
                        });
                    }
                } else if (this.isParams == 2) {
                    if (this.openInfo.ids != undefined) {
                        //修改
                        updateDaProject(
                            {
                                categoryId: this.openInfo.categoryId,
                                id: this.openInfo.ids,
                                column: column
                            }
                        ).then(res => {
                            this.$modal.msgSuccess("修改成功");
                            this.infoList = [];
                            this.value = [];
                            this.uploadValue = undefined
                            this.$emit("submitDetail")
                        })
                    } else {
                        //添加
                        addDaProject(
                            {
                                categoryId: this.openInfo.categoryId,
                                column: column
                            }
                        ).then(res => {
                            this.$modal.msgSuccess("著录成功");
                            this.infoList = [];
                            this.value = [];
                            this.uploadValue = undefined
                            this.$emit("submitDetail")
                        })
                    }
                }else if (this.isParams == 4) {
                    if (this.openInfo.ids != undefined) {
                        //修改
                        updateDaProject(
                            {
                                categoryId: this.openInfo.categoryId,
                                id: this.openInfo.ids,
                                column: column
                            }
                        ).then(res => {
                            this.$modal.msgSuccess("修改成功");
                            this.infoList = [];
                            this.value = [];
                            this.uploadValue = undefined
                            this.$emit("submitDetail")
                        })
                    } else {
                        console.log("sdklfjklsdfjlsdjflskd",this.isParams)
                        //添加
                        addDaProjectjsgl(
                            {
                                categoryId: this.openInfo.categoryId,
                                column: column
                            }
                        ).then(res => {
                            this.$modal.msgSuccess("著录成功");
                            this.infoList = [];
                            this.value = [];
                            this.uploadValue = undefined
                            this.$emit("submitDetail")
                        })
                    }
                } else if (this.isParams == 5) {
                  // 数字化成果接收--档案征集
                  if (this.openInfo.ids != undefined) {
                    //修改
                    updateDaProject(
                      {
                        categoryId: this.openInfo.categoryId,
                        id: this.openInfo.ids,
                        column: column
                      }
                    ).then(res => {
                      this.$modal.msgSuccess("修改成功");
                      this.infoList = [];
                      this.value = [];
                      this.uploadValue = undefined
                      this.$emit("submitDetail")
                    })
                  } else {
                    //添加
                    addZjItem(
                      {
                        categoryId: this.openInfo.categoryId,
                        column: column
                      }
                    ).then(res => {
                      this.$modal.msgSuccess("著录成功");
                      this.infoList = [];
                      this.value = [];
                      this.uploadValue = undefined
                      this.$emit("submitDetail")
                    })
                  }
                } else {
                    if (this.openInfo.ids != undefined) {
                        //修改
                        updateItem1(
                            {
                                categoryId: this.openInfo.categoryId,
                                id: this.openInfo.ids,
                                column: column
                            }
                        ).then(res => {
                            this.$modal.closeLoading()
                            this.$modal.msgSuccess("修改成功");
                            this.infoList = [];
                            this.value = [];
                            this.uploadValue = undefined
                            this.$emit("submitDetail")
                        }).catch(res => {
                            this.$modal.closeLoading()
                        })
                    } else {
                        //添加
                        addProject(
                            {
                                categoryId: this.openInfo.categoryId,
                                column: column
                            }
                        ).then(res => {
                            this.$modal.closeLoading()
                            this.$modal.msgSuccess("著录成功");
                            this.infoList = [];
                            this.value = [];
                            this.uploadValue = undefined
                            this.$emit("submitDetail")
                        }).catch(res => {
                            this.$modal.closeLoading()
                        });
                    }
                }
            }

        }

    }
}
</script>
<style scoped>
::v-deep .el-dialog__body {
    padding: 0 20px 10px !important;
}

::v-deep .leftMain {
    overflow: auto;
    height: 670px;
}
</style>
